﻿using Activity.Data;
using Activity.Helper;
using Activity.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;

namespace Activity.API
{
    /// <summary>
    /// 门店巡检-问题跟进应用接口
    /// </summary>
    public class FollowHandler
    {
        /// <summary>
        /// 获取店铺信息
        /// </summary>
        /// <param name="shopNames">店铺名称</param>
        /// <returns></returns>
        public IList<VShopModel.ShopModel> GetShopList(int? userId)
        {
            List<VShopModel.ShopModel> list = new List<VShopModel.ShopModel>();
            using (ActivityEntities activityEntities = new ActivityEntities())
            {
                var query = activityEntities.t_inspection_finishMaster.Where(c => c.tif_progress == 1 && c.isRemove == false).Select(c => new VShopModel.ShopModel() { ShopCode = c.tif_shopCode, ShopName = c.tif_shopName }).Distinct();
                if (userId > 0)
                {
                    var shopCodes = activityEntities.V_UserShopDataRights.Where(c => c.userid == userId).Select(c => c.shopcode).Distinct().ToArray();
                    query = query.Where(c => shopCodes.Any(d => d == c.ShopCode));
                }
                list = query.ToList();
            }
            return list;
        }
        /// <summary>
        /// 增加问题跟进主单
        /// </summary>
        /// <param name="followMaster"></param>
        /// <returns></returns>
        public FollowModelVO SavaFollow(FollowModelVO VO)
        {
            FollowModelVO followModelVO = null;
            if (VO != null)
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    var t_fm = activityEntities.t_inspection_followMaster.FirstOrDefault(c => c.tf_id == VO.tf_id);
                    if (t_fm == null)
                    {
                        t_fm = new t_inspection_followMaster();
                        t_fm.tif_id = VO.tif_id ?? 0;
                        t_fm.creator = VO.creator ?? 0;
                        t_fm.create_date = DateTime.Now;
                        t_fm.tf_rectificationTime = VO.tf_rectificationTime ?? DateTime.Now;
                        t_fm.tf_shopType = VO.tf_shopType ?? 0;
                        t_fm.tf_sm_date = VO.tf_sm_date;
                        t_fm.tf_sm_name = VO.tf_sm_name;
                        t_fm.tf_sm_reCheckRemarks = VO.tf_sm_reCheckRemarks;
                        t_fm.tf_sv_date = VO.tf_sv_date;
                        t_fm.tf_sv_name = VO.tf_sv_name;
                        t_fm.tf_sv_reCheckRemarks = VO.tf_sv_reCheckRemarks;
                        activityEntities.t_inspection_followMaster.Add(t_fm);
                        activityEntities.SaveChanges();
                        if (VO.DetailVOList != null && VO.DetailVOList.Count > 0)
                        {
                            foreach (var item in VO.DetailVOList)
                            {
                                item.tf_id = t_fm.tf_id;
                            }
                        }
                        SavaDetails(activityEntities, VO.DetailVOList);
                    }
                    else
                    {
                        t_fm.modify_date = DateTime.Now;
                        t_fm.creator = VO.creator ?? 0;
                        t_fm.tf_rectificationTime = VO.tf_rectificationTime ?? DateTime.Now;
                        t_fm.tf_shopType = VO.tf_shopType ?? 0;
                        t_fm.tf_sm_date = VO.tf_sm_date;
                        t_fm.tf_sm_name = VO.tf_sm_name;
                        t_fm.tf_sm_reCheckRemarks = VO.tf_sm_reCheckRemarks;
                        t_fm.tf_sv_date = VO.tf_sv_date;
                        t_fm.tf_sv_name = VO.tf_sv_name;
                        t_fm.tf_sv_reCheckRemarks = VO.tf_sv_reCheckRemarks;
                        activityEntities.Entry<t_inspection_followMaster>(t_fm).State = System.Data.Entity.EntityState.Modified;
                        activityEntities.SaveChanges();
                        SavaDetails(activityEntities, VO.DetailVOList);

                    }
                    var fm = activityEntities.t_inspection_finishMaster.FirstOrDefault(c => c.tif_id == t_fm.tif_id);
                    followModelVO = GetFollowMaster(fm.tif_shopCode);
                }
            }

            return followModelVO;
        }
        /// <summary>
        /// 保存问题跟进明细
        /// </summary>
        /// <param name="activityEntities"></param>
        /// <param name="detailVOList"></param>
        private void SavaDetails(ActivityEntities activityEntities, IList<FollowDetailVO> detailVOList)
        {
            if (detailVOList != null && detailVOList.Count > 0)
            {
                foreach (var item in detailVOList)
                {
                    var tfd_model = activityEntities.t_inspection_followDetail.FirstOrDefault(c => c.tfd_id == item.tfd_id);
                    if (tfd_model == null)
                    {
                        tfd_model = new t_inspection_followDetail { tfd_correctiveRecommendations = item.tfd_correctiveRecommendations, tfd_finish_date = item.tfd_finish_date, tf_id = item.tf_id.Value, tid_id = item.tid_id.Value, tfd_responsible = item.tfd_responsible };
                        //activityEntities.t_inspection_followDetail.Add(tfd_model);
                        activityEntities.Entry(tfd_model).State = EntityState.Added;
                        activityEntities.SaveChanges();
                        SavaImgs(activityEntities, item, tfd_model);
                    }
                    else
                    {
                        tfd_model.tf_id = item.tf_id ?? 0;
                        tfd_model.tid_id = item.tid_id ?? 0;
                        tfd_model.tfd_correctiveRecommendations = item.tfd_correctiveRecommendations;
                        tfd_model.tfd_finish_date = item.tfd_finish_date;
                        tfd_model.tfd_responsible = item.tfd_responsible;
                        activityEntities.Entry<t_inspection_followDetail>(tfd_model).State = System.Data.Entity.EntityState.Modified;
                        activityEntities.SaveChanges();
                        SavaImgs(activityEntities, item, tfd_model);
                    }
                }
            }
        }
        /// <summary>
        /// 保存图片信息
        /// </summary>
        /// <param name="activityEntities"></param>
        /// <param name="item"></param>
        /// <param name="tfd_model"></param>
        private void SavaImgs(ActivityEntities activityEntities, FollowDetailVO item, t_inspection_followDetail tfd_model)
        {
            if (item.kmImgVOList != null && item.kmImgVOList.Count > 0)
            {
                foreach (var imgItem in item.kmImgVOList)
                {
                    Guid guid = new Guid(imgItem.ImageId);
                    var img_model = activityEntities.t_inspection_img.FirstOrDefault(c => c.tid_id == tfd_model.tfd_id && c.Img_ID == guid && c.img_type == 2);
                    if (img_model == null)
                    {
                        img_model = new t_inspection_img { tid_id = tfd_model.tfd_id, Img_ID = guid, img_type = 2 };
                        activityEntities.t_inspection_img.Add(img_model);
                        activityEntities.SaveChanges();
                    }
                    else
                    {
                        img_model.Img_ID = guid;
                        img_model.tid_id = tfd_model.tfd_id;
                        img_model.img_type = 2;
                        activityEntities.Entry(img_model).State = System.Data.Entity.EntityState.Modified;
                        activityEntities.SaveChanges();
                    }
                }
            }
        }
        /// <summary>
        /// 获取问题跟进明细
        /// </summary>
        /// <param name="tf_id">主单ID</param>
        /// <returns></returns>
        public IList<t_inspection_followDetail> GetFollowDetail(int tf_id)
        {
            IList<t_inspection_followDetail> list = new List<t_inspection_followDetail>();
            if (tf_id > 0)
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    list = activityEntities.t_inspection_followDetail.Where(c => c.tf_id == tf_id).ToList();
                }
            }
            return list;
        }
        /// <summary>
        /// 获取最新一次店铺巡检主单
        /// </summary>
        /// <param name="_shopCode">店铺编码</param>
        /// <returns></returns>
        public FollowModelVO GetFollowMaster(string _shopCode)
        {
            FollowModelVO masterModel = null;
            if (!string.IsNullOrEmpty(_shopCode))
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    var temp_model = activityEntities.V_ShopList.Where(c => c.shopcode == _shopCode).Select(c => new { c.BigAreaName, c.AreaName }).FirstOrDefault();
                    string AreaName = string.Format("{0}/{1}", temp_model.BigAreaName, temp_model.AreaName);
                    //查询问题跟进主单
                    var followMaster = activityEntities.t_inspection_followMaster.Where(c => c.t_inspection_finishMaster.tif_shopCode == _shopCode && c.t_inspection_finishMaster.tif_progress == 1 && c.t_inspection_finishMaster.isRemove == false).OrderByDescending(c => c.t_inspection_finishMaster.tif_checkDate).FirstOrDefault();
                    if (followMaster == null)
                    {
                        //创建新单
                        followMaster = new t_inspection_followMaster();
                        followMaster.tf_rectificationTime = DateTime.Now;
                    }
                    //查询巡检主单
                    var finishMaster = activityEntities.t_inspection_finishMaster.Where(c => c.tif_shopCode == _shopCode && c.tif_progress == 1 && c.isRemove == false).OrderByDescending(c => c.tif_checkDate).FirstOrDefault();
                    if (finishMaster != null)
                    {
                        var nowFm = activityEntities.V_t_inspection_finishMaster.FirstOrDefault(c => c.tif_id == finishMaster.tif_id);
                        if (nowFm != null)
                        {
                            finishMaster.tif_score = nowFm.new_tif_score;
                            finishMaster.tif_questionCount = nowFm.ProblemNum;
                        }
                        followMaster.tif_id = finishMaster.tif_id;
                        followMaster.t_inspection_finishMaster = finishMaster;
                    }
                    //填充问题跟进明细
                    IList<FollowDetailVO> DetailVOList = new List<FollowDetailVO>();
                    //巡检明细
                    var wids = activityEntities.t_ins_workorderMaster.Where(c => c.w_type == 3).Select(c => c.w_id).ToArray();
                    var finishDetails = activityEntities.t_inspection_finishDetail.Where(c => c.tif_id == followMaster.tif_id && c.tid_points > 0 &&
                    !activityEntities.t_ins_workorderMaster.Where(w => w.w_type == 3).Any(w => w.tid_id == c.tid_id)).ToList();
                    if (finishDetails != null && finishDetails.Count > 0)
                    {
                        foreach (var item in finishDetails)
                        {
                            var voModel = new FollowDetailVO();
                            //基础资料
                            var insSetModel = activityEntities.t_inspection_setting.FirstOrDefault(c => c.ins_id == item.ins_id);
                            if (insSetModel != null)
                            {
                                voModel.tfd_question = insSetModel.ins_checkContext;
                            }
                            voModel.tid_id = item.tid_id;
                            voModel.tid_remarks = item.tid_remarks;
                            voModel.tid_points = item.tid_points;
                            voModel.tidImgVoList = GetInsFinishImg(item.tid_id);

                            var followDetail = activityEntities.t_inspection_followDetail.FirstOrDefault(c => c.tid_id == item.tid_id);
                            if (followDetail != null)
                            {
                                voModel.tfd_correctiveRecommendations = followDetail.tfd_correctiveRecommendations;
                                voModel.tfd_finish_date = followDetail.tfd_finish_date;
                                voModel.tfd_responsible = followDetail.tfd_responsible;
                                voModel.tf_id = followDetail.tf_id;
                                voModel.tfd_id = followDetail.tfd_id;
                                voModel.kmImgVOList = GetInsImg(followDetail.tfd_id);
                            }
                            //填充
                            DetailVOList.Add(voModel);
                        }
                    }
                    masterModel = GetFollowModelVO(AreaName, followMaster, DetailVOList);
                }
            }
            return masterModel;
        }
        private FollowModelVO GetFollowModelVO(string AreaName, t_inspection_followMaster followMaster, IList<FollowDetailVO> list)
        {
            FollowModelVO followModelVO = new FollowModelVO();
            followModelVO.AreaName = AreaName;
            var finishModel = followMaster.t_inspection_finishMaster;
            followModelVO.tf_id = followMaster.tf_id;
            followModelVO.tif_id = finishModel.tif_id;
            followModelVO.tif_checkDate = finishModel.tif_checkDate;
            followModelVO.tf_rectificationTime = followMaster.tf_rectificationTime == null ? DateTime.Now : followMaster.tf_rectificationTime;
            followModelVO.tf_shopType = followMaster.tf_shopType;
            followModelVO.tif_questionCount = finishModel.tif_questionCount;
            followModelVO.tif_score = finishModel.tif_score;
            followModelVO.tif_shopLevel = finishModel.tif_shopLevel;
            followModelVO.tif_shopManager = finishModel.tif_shopManager;
            followModelVO.tif_shopName = finishModel.tif_shopName;
            followModelVO.tif_storeManager = finishModel.tif_storeManager;
            followModelVO.tf_id = followMaster.tf_id;
            followModelVO.tf_sm_name = followMaster.tf_sm_name;
            followModelVO.tf_sm_reCheckRemarks = followMaster.tf_sm_reCheckRemarks;
            followModelVO.tf_sv_name = followMaster.tf_sv_name;
            followModelVO.tf_sv_reCheckRemarks = followMaster.tf_sv_reCheckRemarks;
            followModelVO.tif_shopLevel = finishModel.tif_shopLevel;
            followModelVO.modify_date = followMaster.modify_date;
            followModelVO.tf_sm_date = followMaster.tf_sm_date;
            followModelVO.tf_sv_date = followMaster.tf_sv_date;
            followModelVO.DetailVOList = list;
            return followModelVO;
        }
        /// <summary>
        /// 或者问题跟进图片信息
        /// </summary>
        /// <param name="tf_id">问题跟进主单ID</param>
        /// <returns></returns>
        public IList<t_inspection_img> GetInspectionImg(int tf_id)
        {
            IList<t_inspection_img> list = new List<t_inspection_img>();
            if (tf_id > 0)
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    list = activityEntities.t_inspection_img.Where(c => c.tid_id == tf_id && c.img_type == 2).ToList();
                }
            }
            return list;
        }
        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="tf_id"></param>
        /// <returns></returns>
        public IList<KmImgVO> GetInsImg(int tid_id)
        {
            IList<KmImgVO> imgs = new List<KmImgVO>();
            if (tid_id > 0)
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    var items = activityEntities.t_inspection_img.Where(c => c.tid_id == tid_id && c.img_type == 2).Join(activityEntities.kmImages, s => s.Img_ID, a => a.ID, (s, a) => new { ImageId = a.ID, tf_id = s.ID, a.Url, a.BucketName }).Take(3).ToList();
                    foreach (var item in items)
                    {
                        string imgId = item.ImageId.ToString();
                        imgs.Add(new KmImgVO { ImageId = imgId, tf_id = item.tf_id, Url = ImageHelper.GetOssUrl(item.BucketName, item.Url) });
                    }
                }
            }
            return imgs;
        }
        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="tf_id"></param>
        /// <returns></returns>
        public IList<KmImgVO> GetInsFinishImg(int tid_id)
        {
            IList<KmImgVO> imgs = new List<KmImgVO>();
            if (tid_id > 0)
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    var items = activityEntities.t_inspection_img.Where(c => c.tid_id == tid_id && c.img_type == 1).Join(activityEntities.kmImages, s => s.Img_ID, a => a.ID, (s, a) => new { ImageId = a.ID, tf_id = s.ID, a.Url, a.BucketName }).Take(3).ToList();
                    foreach (var item in items)
                    {
                        string imgId = item.ImageId.ToString();
                        imgs.Add(new KmImgVO { ImageId = imgId, tf_id = item.tf_id, Url = ImageHelper.GetOssUrl(item.BucketName, item.Url) });
                    }
                }
            }
            return imgs;
        }
        /// <summary>
        /// 删除跟进明细
        /// </summary>
        /// <param name="tfd_id">明细单ID</param>
        /// <returns></returns>
        public bool RemoveDetail(int tfd_id)
        {
            bool result = false;
            if (tfd_id > 0)
            {
                using (ActivityEntities activityEntities = new ActivityEntities())
                {
                    var fd_model = activityEntities.t_inspection_followDetail.FirstOrDefault(c => c.tfd_id == tfd_id);
                    if (fd_model != null)
                    {
                        var fi_models = activityEntities.t_inspection_img.Where(c => c.tid_id == tfd_id && c.img_type == 2).ToList();
                        if (fi_models != null && fi_models.Count > 0)
                        {
                            foreach (var item in fi_models)
                            {
                                var img_model = activityEntities.kmImages.FirstOrDefault(c => c.ID == item.Img_ID);
                                if (img_model != null)
                                {
                                    //删除图片
                                    ImageHelper.DeleteOssImg(img_model.BucketName, img_model.Url);
                                    activityEntities.kmImages.Remove(img_model);
                                    activityEntities.SaveChanges();
                                }
                                activityEntities.t_inspection_img.Remove(item);
                                activityEntities.SaveChanges();
                            }
                        }
                        activityEntities.t_inspection_followDetail.Remove(fd_model);
                        activityEntities.SaveChanges();
                        //删除图片
                        result = true;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="_shopCode"></param>
        /// <returns></returns>
        /// writer:ljj
        /// createdate:2018-03-23
        public string ExportFollowMaster(string _shopCode)
        {
            try
            {
                var strPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取或设置包含该应用程序的目录的名称。
                if (!Directory.Exists(strPath + "upfile"))
                {
                    Directory.CreateDirectory(strPath + "upfile"); //不存在就创建文件夹 }
                }
                if (!Directory.Exists(strPath + "upfile\\download"))
                {
                    Directory.CreateDirectory(strPath + "upfile\\download"); //不存在就创建文件夹 }
                }
                var dyInfo = new DirectoryInfo(strPath + "upfile\\download");

                var fileName = $"{_shopCode}_店铺巡检问题跟进表.doc";

                var filePath = $"{strPath}upfile\\download\\{fileName}";
                var filePath2 = $"upfile\\download\\{fileName}";

                if (!File.Exists(filePath))
                {
                    var fmBase = GetFollowMaster(_shopCode);

                    if (fmBase != null)
                    {
                        //创建document对象  
                        var doc = new XWPFDocument();

                        //创建段落对象  
                        var title1 = doc.CreateParagraph();
                        title1.Alignment = ParagraphAlignment.CENTER; //字体居中  

                        //创建run对象  
                        //本节提到的所有样式都是基于XWPFRun的，  
                        //你可以把XWPFRun理解成一小段文字的描述对象，  
                        //这也是Word文档的特征，即文本描述性文档。  
                        //来自Tony Qu http://tonyqus.sinaapp.com/archives/609  
                        var runTitle1 = title1.CreateRun();
                        runTitle1.SetBold(true);
                        runTitle1.SetText("店铺巡检问题跟进表");
                        runTitle1.FontSize = 25;
                        runTitle1.FontFamily = "宋体";

                        var tableShopBase = doc.CreateTable(3, 6);
                        tableShopBase.Width = (int)(1000 * 5);
                        //tableShopBase.SetColumnWidth(0, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(1, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(2, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(3, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(4, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(5, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(6, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(7, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(8, 716); /* 设置列宽 */
                        //tableShopBase.SetColumnWidth(9, 716); /* 设置列宽 */

                        tableShopBase.GetRow(0).GetCell(0).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "门店名称",
                            ParagraphAlignment.CENTER, TextAlignment.AUTO, 10, true, 8));
                        tableShopBase.GetRow(0).GetCell(1).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tif_shopName, ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, false, 8));
                        tableShopBase.GetRow(0).GetCell(2).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "店长",
                            ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(0).GetCell(3).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tif_storeManager, ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, false, 8));
                        tableShopBase.GetRow(0).GetCell(4).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "所属区域",
                            ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(0).GetCell(5).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.AreaName, ParagraphAlignment.CENTER, TextAlignment.CENTER, 10,
                            false, 8));

                        tableShopBase.GetRow(1).GetCell(0).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "店经理", ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(1).GetCell(1).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tif_shopManager, ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, false, 8));
                        tableShopBase.GetRow(1).GetCell(2).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "整改时间", ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(1).GetCell(3).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tf_rectificationTime?.ToString("yyyy-MM-dd") ?? "---", ParagraphAlignment.CENTER,
                            TextAlignment.CENTER, 10, false, 8));
                        tableShopBase.GetRow(1).GetCell(4).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "门店类型", ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(1).GetCell(5).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tf_shopType == 1 ? "街面店" : "商场店", ParagraphAlignment.CENTER, TextAlignment.CENTER,
                            10, false, 8));

                        tableShopBase.GetRow(2).GetCell(0).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "巡店得分", ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(2).GetCell(1).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tif_score.ToString(), ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, false, 8,
                            "宋体", "#ff0000"));
                        tableShopBase.GetRow(2).GetCell(2).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "本次巡店问题数", ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(2).GetCell(3).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tif_questionCount.ToString(), ParagraphAlignment.CENTER, TextAlignment.CENTER, 10,
                            false, 8, "宋体", "#ff0000"));
                        tableShopBase.GetRow(2).GetCell(4).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            "店铺等级", ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                        tableShopBase.GetRow(2).GetCell(5).SetParagraph(ExportHelper.SetCellText(doc, tableShopBase,
                            fmBase.tif_shopLevel, ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, false, 8));

                        if (fmBase.DetailVOList.Any())
                        {
                            var title2 = doc.CreateParagraph();
                            title2.Alignment = ParagraphAlignment.LEFT;
                            var runTitle2 = title2.CreateRun();
                            runTitle2.SetText("问题列表");
                            runTitle2.FontSize = 8;
                            runTitle2.FontFamily = "宋体";
                            runTitle2.SetBold(true);

                            var tableQuestion = doc.CreateTable(fmBase.DetailVOList.Count() + 1, 7);
                            tableQuestion.Width = (int)(1000 * 5);

                            tableQuestion.GetRow(0).GetCell(0).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "问题描述",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                            tableQuestion.GetRow(0).GetCell(1).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "备注",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                            tableQuestion.GetRow(0).GetCell(2).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "图片",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                            tableQuestion.GetRow(0).GetCell(3).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "整改措施及处罚情况",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                            tableQuestion.GetRow(0).GetCell(4).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "上传图片",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                            tableQuestion.GetRow(0).GetCell(5).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "完成时间",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));
                            tableQuestion.GetRow(0).GetCell(6).SetParagraph(ExportHelper.SetCellText(doc, tableQuestion,
                                "责任人",
                                ParagraphAlignment.CENTER, TextAlignment.CENTER, 10, true, 8));

                            var i = 1;
                            foreach (var item in fmBase.DetailVOList)
                            {
                                var para = new CT_P();
                                var itemQuestion = new XWPFParagraph(para, tableQuestion.Body);
                                itemQuestion.Alignment = ParagraphAlignment.LEFT;
                                var runItemQuestion = itemQuestion.CreateRun();
                                runItemQuestion.SetText(item.tfd_question);
                                runItemQuestion.FontSize = 8;
                                runItemQuestion.FontFamily = "宋体";
                                runItemQuestion.SetTextPosition(10);
                                runItemQuestion = itemQuestion.CreateRun();
                                runItemQuestion.SetText($"({item.tid_points})分");
                                runItemQuestion.FontSize = 8;
                                runItemQuestion.FontFamily = "宋体";
                                runItemQuestion.SetTextPosition(10);
                                runItemQuestion.SetColor("#ff0000");

                                tableQuestion.GetRow(i).GetCell(0).SetParagraph(itemQuestion);
                                tableQuestion.GetRow(i).GetCell(1).SetParagraph(ExportHelper.SetCellText(doc,
                                    tableQuestion, item.tid_remarks, ParagraphAlignment.LEFT, TextAlignment.CENTER, 10,
                                    true, 8));
                                if (item.tidImgVoList != null && item.tidImgVoList.Any())
                                {
                                    tableQuestion.GetRow(i).GetCell(2).SetParagraph(ExportHelper.SetCellPicture(doc,
                                        tableQuestion, item.tidImgVoList.Select(d => d.Url).ToList(),
                                        item.tidImgVoList.Select(d => d.ImageId).ToList(), ParagraphAlignment.LEFT,
                                        (int)(100.0 * 9525), (int)(100.0 * 9525)));
                                }
                                else
                                {
                                    tableQuestion.GetRow(i).GetCell(2).SetParagraph(ExportHelper.SetCellText(doc,
                                        tableQuestion, "", ParagraphAlignment.LEFT, TextAlignment.CENTER, 10, true, 8));
                                }
                                tableQuestion.GetRow(i).GetCell(3).SetParagraph(ExportHelper.SetCellText(doc,
                                    tableQuestion,
                                    string.IsNullOrEmpty(item.tfd_correctiveRecommendations)
                                        ? ""
                                        : item.tfd_correctiveRecommendations, ParagraphAlignment.LEFT,
                                    TextAlignment.CENTER, 10, true, 8));
                                if (item.kmImgVOList != null && item.kmImgVOList.Any())
                                {
                                    tableQuestion.GetRow(i).GetCell(4).SetParagraph(ExportHelper.SetCellPicture(doc,
                                        tableQuestion, item.kmImgVOList.Select(d => d.Url).ToList(),
                                        item.kmImgVOList.Select(d => d.ImageId).ToList(), ParagraphAlignment.LEFT,
                                        (int)(100.0 * 9525), (int)(100.0 * 9525)));
                                }
                                else
                                {
                                    tableQuestion.GetRow(i).GetCell(4).SetParagraph(ExportHelper.SetCellText(doc,
                                        tableQuestion, "", ParagraphAlignment.LEFT, TextAlignment.CENTER, 10, true, 8));
                                }
                                tableQuestion.GetRow(i).GetCell(5).SetParagraph(ExportHelper.SetCellText(doc,
                                    tableQuestion,
                                    item.tfd_finish_date?.ToString("yyyy-MM-dd") ?? "---", ParagraphAlignment.LEFT,
                                    TextAlignment.CENTER, 10, true, 8));
                                tableQuestion.GetRow(i).GetCell(6).SetParagraph(ExportHelper.SetCellText(doc,
                                    tableQuestion,
                                    string.IsNullOrEmpty(item.tfd_responsible) ? "" : item.tfd_responsible,
                                    ParagraphAlignment.LEFT, TextAlignment.CENTER, 10, true, 8));
                                i++;
                            }

                            var titleSMRemark = doc.CreateParagraph();
                            titleSMRemark.Alignment = ParagraphAlignment.LEFT;
                            var runTitleSMRemark = titleSMRemark.CreateRun();
                            runTitleSMRemark.SetText($"复查情况:{fmBase.tf_sm_reCheckRemarks}");
                            runTitleSMRemark.FontSize = 12;
                            runTitleSMRemark.FontFamily = "宋体";
                            runTitleSMRemark.SetTextPosition(20);

                            var titleSM = doc.CreateParagraph();
                            titleSM.Alignment = ParagraphAlignment.RIGHT;
                            var runTitleSM = titleSM.CreateRun();
                            runTitleSM.SetText("店经理(区域经理)：");
                            runTitleSM.FontSize = 10;
                            runTitleSM.FontFamily = "宋体";
                            runTitleSM.SetTextPosition(15);

                            runTitleSM = titleSM.CreateRun();
                            runTitleSM.SetText($"{fmBase.tf_sm_name}             ");
                            runTitleSM.FontSize = 10;
                            runTitleSM.FontFamily = "宋体";
                            runTitleSM.SetTextPosition(15);
                            runTitleSM.SetUnderline(UnderlinePatterns.Single);

                            runTitleSM = titleSM.CreateRun();
                            runTitleSM.SetText("            复查时间：");
                            runTitleSM.FontSize = 10;
                            runTitleSM.FontFamily = "宋体";
                            runTitleSM.SetTextPosition(15);

                            runTitleSM = titleSM.CreateRun();
                            runTitleSM.SetText(
                                $"{(fmBase.tf_sm_date.HasValue ? $"{fmBase.tf_sm_date.Value:yyyy-MM-dd}" : "")}            ");
                            runTitleSM.FontSize = 10;
                            runTitleSM.FontFamily = "宋体";
                            runTitleSM.SetTextPosition(15);
                            runTitleSM.SetUnderline(UnderlinePatterns.Single);

                            var titleSVRemark = doc.CreateParagraph();
                            titleSVRemark.Alignment = ParagraphAlignment.LEFT;
                            var runTitleSVRemark = titleSVRemark.CreateRun();
                            runTitleSVRemark.SetText($"复查情况:{fmBase.tf_sv_reCheckRemarks}");
                            runTitleSVRemark.FontSize = 12;
                            runTitleSVRemark.FontFamily = "宋体";
                            runTitleSVRemark.SetTextPosition(20);

                            var titleSV = doc.CreateParagraph();
                            titleSV.Alignment = ParagraphAlignment.RIGHT;
                            var runTitleSV = titleSV.CreateRun();
                            runTitleSV.SetText("督导：");
                            runTitleSV.FontSize = 10;
                            runTitleSV.FontFamily = "宋体";
                            runTitleSV.SetTextPosition(15);

                            runTitleSV = titleSV.CreateRun();
                            runTitleSV.SetText($"{fmBase.tf_sv_name}            ");
                            runTitleSV.FontSize = 10;
                            runTitleSV.FontFamily = "宋体";
                            runTitleSV.SetTextPosition(15);
                            runTitleSV.SetUnderline(UnderlinePatterns.Single);

                            runTitleSV = titleSV.CreateRun();
                            runTitleSV.SetText("            复查时间：");
                            runTitleSV.FontSize = 10;
                            runTitleSV.FontFamily = "宋体";
                            runTitleSV.SetTextPosition(15);

                            runTitleSV = titleSV.CreateRun();
                            runTitleSV.SetText(
                                $"{(fmBase.tf_sv_date.HasValue ? $"{fmBase.tf_sv_date.Value:yyyy-MM-dd}" : "")}            ");
                            runTitleSV.FontSize = 10;
                            runTitleSV.FontFamily = "宋体";
                            runTitleSV.SetTextPosition(15);
                            runTitleSV.SetUnderline(UnderlinePatterns.Single);
                        }

                        var fs = new FileStream(filePath, FileMode.Create);
                        doc.Write(fs);
                        fs.Close();

                        //获取文件夹下所有的文件
                        foreach (FileInfo feInfo in dyInfo.GetFiles())
                        {
                            //判断文件日期是否小于2小时，是则删除
                            if (feInfo.CreationTime < DateTime.Now.AddHours(-1))
                                feInfo.Delete();
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                return filePath2;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }
    }
    /// <summary>
    /// 问题跟进主单--服务前端Model
    /// </summary>
    [Serializable]
    public class FollowModelVO
    {
        public int? tf_id { get; set; }
        public int? tif_id { get; set; }
        /// <summary>
        /// 巡检时间
        /// </summary>
        public DateTime? tif_checkDate { get; set; }
        /// <summary>
        /// 店铺区域信息
        /// </summary>
        public string AreaName { get; set; }
        public string tif_shopName { get; set; }
        public string tif_storeManager { get; set; }
        public string tif_shopManager { get; set; }
        public DateTime? tf_rectificationTime { get; set; }
        public decimal? tif_score { get; set; }
        public int? tf_shopType { get; set; }
        public int? tif_questionCount { get; set; }
        public string tif_shopLevel { get; set; }
        public int? creator { get; set; }
        public DateTime? create_date { get; set; }
        public DateTime? modify_date { get; set; }
        public string tf_sm_reCheckRemarks { get; set; }
        public string tf_sm_name { get; set; }
        public DateTime? tf_sm_date { get; set; }
        public string tf_sv_reCheckRemarks { get; set; }
        public string tf_sv_name { get; set; }
        public DateTime? tf_sv_date { get; set; }
        public IList<FollowDetailVO> DetailVOList { get; set; }
    }
    /// <summary>
    /// 问题跟进明细--服务前端Model
    /// </summary>
    [Serializable]
    public class FollowDetailVO
    {
        /// <summary>
        /// 主键
        /// </summary>
        public int? tfd_id { get; set; }
        /// <summary>
        /// 问题跟进主单
        /// </summary>
        public int? tf_id { get; set; }
        /// <summary>
        /// 问题处罚情况
        /// </summary>
        public string tfd_correctiveRecommendations { get; set; }
        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime? tfd_finish_date { get; set; }
        /// <summary>
        /// 责任人
        /// </summary>
        public string tfd_responsible { get; set; }
        /// <summary>
        /// 图片
        /// </summary>
        public IList<KmImgVO> kmImgVOList { get; set; }
        /// <summary>
        /// 问题ID
        /// </summary>
        public int? tid_id { get; set; }
        /// <summary>
        /// 检查项目ins_checkContext
        /// </summary>
        public string tfd_question { get; set; }
        /// <summary>
        /// 扣分
        /// </summary>
        public decimal? tid_points { get; set; }
        /// <summary>
        /// 问题描述
        /// </summary>
        public string tid_remarks { get; set; }
        /// <summary>
        /// 扣分图片
        /// </summary>
        public IList<KmImgVO> tidImgVoList { get; set; }
    }
    /// <summary>
    /// 问题跟进图片--服务前端Model
    /// </summary>
    [Serializable]
    public class KmImgVO
    {
        public string Url { get; set; }
        public int? tf_id { get; set; }
        public string ImageId { get; set; }
    }
}
